<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Licensed ( https://opensource.org/licenses/MIT )
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// +----------------------------------------------------------------------
// | 统一中间键配置
// +----------------------------------------------------------------------
return [
    //------------------------------ 服务
    //服务启动前，统一参数：(\Inphp\Core\Services\Server $server)
    'beforeStart'   => [
        //可以运行模块初始化，加载各模块文件，如果运行于swoole拓展下，该部分加载的配置将无法热更新
        [\Inphp\Core\Modules::class, "init"]
    ],
    //主服务进程启动时(仅在swoole server有效)
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\Server $swooleServer)
    'onStart'       => [],
    //子进程启动(仅在swoole server有效)
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\Server $swooleServer, int $workerId)
    'onWorkerStart' => [
        //让所有worker都有一个PDO连接池，按需使用，因为会产生数据库连接消耗
        [\Inphp\Core\Db\Db::class, "init"],
        //所有worker初始化一个Redis连接池
        [\Inphp\Core\Db\Redis::class, "init"]
    ],
    //客户端连接
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\Server $swooleServer, int $fd, int $reactorId)
    'onConnect'     => [],
    //处理过程中，未能获取到客户端数据，即未知客户端
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\WebSocket\Server $swooleWSServer, \Swoole\WebSocket\Frame $frame)
    'onUnknownClient' => [],
    //------------------------------ http请求部分
    //接收http请求
    //统一参数：(\Inphp\Core\Services\Server $server, \Inphp\Core\Services\Response $response)
    'onRequest'     => [],
    //响应处理错误
    //统一参数：(\Inphp\Core\Services\Server $server, \Exception $exception)
    'onRequestException' => [],
    //------------------------------ ws部分
    //客户端连接并握手完成
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\Server $swooleServer, \Swoole\Http\Request $swooleHttpRequest)
    'onOpen'        => [],
    //客户端连接关闭，可能是客户端自己关闭，也可能是服务端主动踢掉
    //统一参数：(\Inphp\Core\Services\Server $server, \Swoole\Server $swooleServer, int $fd, int $reactorId)
    'onClose'       => [],
    //接收到客户端的ws消息，该方法全直接接管系统处理，不会处理路由、控制器
    //统一参数：(\Inphp\Core\Services\Http\Server $server, \Swoole\WebSocket\Server $wsServer, \Swoole\WebSocket\Frame $frame, mixed $data(消息数据))
    'onMessage'     => [],
    //接收到了未知数据
    //统一参数：(\Inphp\Core\Services\Http\Server $server, \Swoole\WebSocket\Server $wsServer, \Swoole\WebSocket\Frame $frame)
    'onUnknownMessage'=> [
        //仅作示例，请自己按需处理掉未知消息
        function () {
            list ($server, $wsServer, $frame) = func_get_args();
            $wsServer->push($frame->fd, (new \Inphp\Core\Object\Message())->event("onUnknownMessage")->error(404)->message($frame->data)->toJson());
        }
    ],
    //路由处理
    'onRouter'      => null,
    //控制器已初始化，但未执行前
    //HTTP请求时，统一传参：(\Inphp\Core\Services\Http\Response, 请求处理的控制器 controller, 执行的控制器方法名 method)
    //WS请求时，统一传参：(\Inphp\Core\Services\Http\Server, 请求处理的控制器 controller, 执行的控制器方法名 method, 接收到的消息)
    'beforeExecute' => [
        //注解实现，该实现方法只接收3个参数：response/server, controller, method，如果放在别的地方，请务必传参正确
        //实现过程统一给注解处理器传3个参数：注解位置类型(class或property或method), 类对象, 注解位置的对象名称(如果是class，此值为空，否则是类的属性名或方法名)
        [\Inphp\Core\Middlewares\Attribute::class, "process"]
    ],
    //http请求，控制器已执行，未响应前，统一传参：\Inphp\Core\Services\Http\Response
    'beforeEnd'    => [
        //可用于实现Smarty模板引擎渲染，此中间件必须放在此处，需要自行引入: composer require smarty/smarty
        [\Inphp\Core\Middlewares\SmartyView::class, "process"]
    ],
    //----------------------------- 异步任务投递
    //投递异步任务(仅在swoole http server有效)
    //统一参数：(\Inphp\Core\Services\Http\Server $server, \Swoole\Server\Task $task)
    'onTask'        => [],
    //异步任务执行完成(仅在swoole http server有效)，需要Task有执行 finish 或 return 结果，否则不会执行
    //统一参数：(\Inphp\Core\Services\Http\Server $server, \Swoole\Server $server, int $task_id, mixed $data)
    'onFinish'      => []
];